package com.qiang.work.test09;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.TreeSet;


//线程
class MyThread extends Thread {
    private final ArrayList<String> list;
//    private final List<String> list;
//    public MyThread(ArrayList<String> list) {
//        this.list = list;
//    }

    Random r = new Random();

    public MyThread(ArrayList<String> list) {
        this.list = list;
    }


    @Override
    public void run() {
        while (true) {

            synchronized (this) {
                if (list.size() == 0) {
                    break;
                }
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //System.out.println(Thread.currentThread().getName());
                System.out.println(list.remove(r.nextInt(list.size())) + Thread.currentThread().getName() + "接收注射");
            }
        }
    }
}


//测试类
public class Deom04 {
    public static void main(String[] args) throws IOException, InterruptedException {

        //读取名单
        BufferedReader br = new BufferedReader(new FileReader("day18\\stuName.txt"));
        //存入集合中，set集合不能重复
        ArrayList<String> list = new ArrayList<>();
        String s;
        while ((s = br.readLine()) != null) {
            list.add(s);
        }
        //System.out.println(list);

//        while (list != null) {
//            Random r = new Random();
//            int i = r.nextInt(list.size());
//            if (list.get(i) == null) {
//                break;
//            }
        MyThread m = new MyThread(list);

        Thread t1 = new Thread(m);
        Thread t2 = new Thread(m);
//            MyThread t1 = new MyThread() {
//                @Override
//                public void run() {
//                    synchronized (MyThread.bo) {
//                        try {
//                            Thread.sleep(2000);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                        String ss = list.remove(i);
//
//                        System.out.println(ss + "小朋友在" + Thread.currentThread().getName() + "接收注射");
//                    }
//                }
//            };
//
//            MyThread t2 = new MyThread() {
//                @Override
//                public void run() {
//                    synchronized (MyThread.bo) {
//                        try {
//                            Thread.sleep(2000);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                        String ss = list.remove(i);
//
//                        System.out.println(ss + "小朋友在" + Thread.currentThread().getName() + "接收注射");
//                    }
//                }
//            };
        t1.setName("窗口一");
        t2.setName("窗口二");
        t1.start();
        t2.start();

    }

}
